<?php
class PluginHandler {
	
	public static function inc() {
		global $pluginsEnabled;
		
		foreach ($pluginsEnabled as $pName) {
				
			$script = PLUGINS_PATH.$pName.'/include.php';
			include_once($script);
			if (file_exists(PLUGINS_PATH.$pName.'/services/routing.php')) {
			    require_once (PLUGINS_PATH.$pName.'/services/routing.php');
                $fname = $pName.'_routing';
                $GLOBALS['ServiceHandler']->appendRouter($fname());
			}
		}		
	}
	
	
	public static function beforeAction($ctrl) {

		global $pluginsEnabled;	
		
		foreach ($pluginsEnabled as $pName) {
			
			$script = PLUGINS_PATH.$pName.'/before.php';
			
			if (!file_exists($script)) continue;
			
			$fname	= $pName.'before';
			include_once($script);
			$fname($ctrl);
			
		}
		
	}

	public static function afterAction($ctrl) {
		global $pluginsEnabled;
		
		foreach ($pluginsEnabled as $pName) {
				
			$script = PLUGINS_PATH.$pName.'/after.php';
				
			if (!file_exists($script)) continue;
				
			$fname	= $pName.'after';
			include_once($script);
			$fname($ctrl);
				
		}	
	}
	
	public static function search($ctrl) {
		global $pluginsEnabled;
		
		foreach ($pluginsEnabled as $pName) {
		
			$script = PLUGINS_PATH.$pName.'/ctrl/'.$ctrl.'.php';
			
			if (file_exists($script)) return $script;
		
		}
		
		return false;
	}
	
	
}